package com.ldcchina.tester.ehcache;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * EhCache缓存初始化
 * @author Winter Lau
 */
public class MyCacheManager implements ServletContextListener {

    public void contextInitialized(ServletContextEvent e) {
        CacheManager ehcache = new CacheManager();
        Cache cache = ehcache.getCache("mycache");
        //初始化缓存数据
        for (long i = 0; i < 10; i++) {
            Employee employee = new Employee(1L, "无忌");
            Element element = new Element(i + "@employee", employee);
            cache.put(element);
        }

        e.getServletContext().setAttribute("cache", cache);

    }

    public void contextDestroyed(ServletContextEvent e) {
        e.getServletContext().removeAttribute("cache");
    }

}
